package com.xybian.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Scanner;

/**
 * MyBatis Plus 生成器
 *
 * @author SongRenShuo
 * @date 2023-12-23
 */
public class MybatisPlusGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        //获取程序当前路径
        String projectPath =  System.getProperty("user.dir");
//        String projectPath = "D:\\java测试目录";
        // 数据源配置
        String dbUrl = "jdbc:mysql://localhost:3306/sys_security?characterEncoding=UTF-8&useUnicode=true&useSSL=false";
        DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig.Builder(dbUrl,"root","12345")
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());

        // 代码生成器
        FastAutoGenerator mpg = FastAutoGenerator.create(dataSourceConfig);
        mpg.globalConfig(globalBuilder -> globalBuilder.fileOverride().disableOpenDir()
                .outputDir(projectPath + "/src/main/java")
                .author("")
                .commentDate("yyyy-MM-dd HH:mm:ss")
                .dateType(DateType.TIME_PACK)
                .enableSwagger());
        mpg.packageConfig(packageBuilder -> packageBuilder
                .parent("com.xybian")
                .service("services")
                .serviceImpl("services.impl")
                .xml("mybatis")
        );



        mpg.strategyConfig(strategyconfigBuilder -> strategyconfigBuilder
                .enableCapitalMode()
                .enableSkipView ()
                .disableSqlFilter()
                .addInclude(scanner("表名"))
        );



        //entity 生成策略
        mpg.strategyConfig(strategyconfigBuilder -> strategyconfigBuilder.entityBuilder()
//								.enableTableFieldAnnotation ()
                        .naming (NamingStrategy.underline_to_camel)
                        .columnNaming (NamingStrategy.underline_to_camel)
                        .idType(IdType.AUTO)
//								.enableLombok ()
                        /*.logicDeleteColumnName ( "deleted ").logicDeletePropertyName ( "deleted ")
                        .addTableFills(new Column( "create_time" , FieldFill.INSERT))
                        .addTableFills(new Property( "updateTime " ,FieldFill.INSERT_UPDATE))*/
                        .versionColumnName ("version")
                        .disableSerialVersionUID()
        );

        //controller 生成策略
        mpg.strategyConfig(
                strategyconfigBuilder ->strategyconfigBuilder
                        .controllerBuilder()
                        .enableRestStyle()
                        .enableHyphenStyle()
        );


        //service 生成策略
        mpg.strategyConfig(
                strategyconfigBuilder -> strategyconfigBuilder
                        .serviceBuilder()
                        .formatServiceFileName ("%sService")
                        .formatServiceImplFileName ("%sServiceImpl"));

        //mapper 生成策略
        mpg.strategyConfig (
                strategyconfigBuilder ->strategyconfigBuilder.mapperBuilder()
                        .formatMapperFileName ("%sMapper")
                        .formatXmlFileName ( "%sMapper" )
                        .enableBaseResultMap());

        mpg.execute();
    }
}
